package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.service.cargo.ContractService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractDao contractDao;

    /*
        购销合同分页查询
     */
    @Override
    public PageInfo<Contract> findByPage(ContractExample contractExample, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum,pageSize);

        //查询数据
        List<Contract> contractList = contractDao.selectByExample(contractExample);

        //得到购销合同页面
        PageInfo<Contract> pageInfo = new PageInfo<>(contractList);


        return pageInfo;
    }

    /*
     购销合同条件查询
  */
    @Override
    public List<Contract> findAll(ContractExample contractExample) {
        List<Contract> contractList = contractDao.selectByExample(contractExample);
        return contractList;
    }

      /*
        根据id查询购销合同
    */

    @Override
    public Contract findById(String id) {

        return contractDao.selectByPrimaryKey(id);
    }

       /*
      添加购销合同
  */

    @Override
    public void save(Contract contract) {

        contract.setId(UUID.randomUUID().toString());

        contract.setCreateTime(new Date());
        contract.setUpdateTime(new Date());

        contractDao.insertSelective(contract);

    }

    /*
 更新购销合同
  */
    @Override
    public void update(Contract contract) {

        contract.setUpdateTime(new Date());
        contractDao.updateByPrimaryKeySelective(contract);
    }

     /*
    根据主键删除购销合同
     */

    @Override
    public void delete(String id) {
        contractDao.deleteByPrimaryKey(id);

    }

    //大区经理查询购销合同
    @Override
    public PageInfo<Contract> findPageByDeptId(String companyId, String deptId, Integer pageNum, Integer pageSize) {

        System.err.println("来到了系统管理员购销合同Service");
        PageHelper.startPage(pageNum,pageSize);
        //查询购销合同的数据
        List<Contract> contractList = contractDao.findByDeptId(companyId,deptId);


        return new PageInfo<>(contractList);
    }
}
